﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Data.SqlClient;

namespace CiroKontrol
{
    public partial class frmMain : XtraForm
    {
        public frmMain()
        {
            InitializeComponent();
        }

        string cnnString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];


        private void CariBilgileri(string cariKod)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("SELECT     CUSTOMERS.name AS [Cari Ünvanı], CAST(SUM(ORDER_DETAILS.total) AS decimal(16, 2)) AS Tutar, CUSTOMERS.adress, CUSTOMERS.telnumber, CUSTOMERS.taxoffice, CUSTOMERS.taxnumber, CUSTOMERS.faxnumber ");
            sb.Append(" FROM         ORDER_DETAILS WITH (NOLOCK) INNER JOIN  CUSTOMERS WITH (NOLOCK) ON ORDER_DETAILS.customer_code = CUSTOMERS.code ");
            sb.Append(" WHERE     (ORDER_DETAILS.customer_code = '{0}') ");
            sb.Append(" GROUP BY CUSTOMERS.name, CUSTOMERS.adress, CUSTOMERS.telnumber, CUSTOMERS.taxoffice, CUSTOMERS.taxnumber, CUSTOMERS.faxnumber ");
            sb.Append(" ORDER BY SUM(ORDER_DETAILS.total) DESC ");

            string cariunvan = "";
            decimal ciro = 0;
            using (SqlConnection cn = new SqlConnection(cnnString))
            {
                cn.Open();
                using (SqlCommand cmd = new SqlCommand(string.Format(sb.ToString(), cariKod), cn))
                {
                    SqlDataReader rd = cmd.ExecuteReader();
                    if (rd.HasRows)
                    {
                        while (rd.Read())
                        {
                            cariunvan = rd.GetString(0) + "\r\n" + rd.GetString(2) + "\r\n" + rd.GetString(4) + "\r\n" + rd.GetString(5) + "\r\n" + rd.GetString(3) + "\r\n" + rd.GetString(6);
                            ciro = rd.GetDecimal(1);
                        }
                    }
                    rd.Close();
                }
                cn.Close();
            }
            txtBilgi.Text = cariunvan;
            lblCiro2.Text = string.Format("{0} TL", ciro.ToString("#,##0.00"));
            TedarikciListe(cariKod);
        }

        public void ColumnReFormat(DevExpress.XtraGrid.Views.Grid.GridView grd)
        {
            for (int i = 0; i <= grd.Columns.Count - 1; i++)
            {
                switch (grd.Columns[i].ColumnType.ToString())
                {
                    case "System.int32":
                        grd.Columns[i].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
                        grd.Columns[i].DisplayFormat.FormatString = "f0";
                        break;
                    case "System.int16":
                        grd.Columns[i].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
                        grd.Columns[i].DisplayFormat.FormatString = "f0";
                        break;
                    case "System.Decimal":
                        grd.Columns[i].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
                        grd.Columns[i].DisplayFormat.FormatString = "n2";
                        break;
                    case "System.Double":
                        grd.Columns[i].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
                        grd.Columns[i].DisplayFormat.FormatString = "n2";
                        break;
                    case "System.Float":
                        grd.Columns[i].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
                        grd.Columns[i].DisplayFormat.FormatString = "n2";
                        break;
                }
            }
        }


        private void TedarikciListe(string cariKod)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" SELECT     CUSTOMERS.name AS [Tedarikçi Firma], CAST(SUM(ORDER_DETAILS.total) AS decimal(16, 2)) AS [Kdv Hariç Net Ciro] ");
            sb.Append(" FROM         ORDER_DETAILS WITH (NOLOCK) INNER JOIN CUSTOMERS WITH (NOLOCK) ON ORDER_DETAILS.supplier = CUSTOMERS.code ");
            sb.Append(" WHERE     (ORDER_DETAILS.customer_code = '{0}') GROUP BY CUSTOMERS.name  ORDER BY [Kdv Hariç Net Ciro] DESC");
            DataTable dt = new DataTable();
            using (SqlConnection cn = new SqlConnection(cnnString))
            {
                cn.Open();
                using (SqlCommand cmd = new SqlCommand(string.Format(sb.ToString(), cariKod), cn))
                {
                    using (SqlDataAdapter adp = new SqlDataAdapter(cmd))
                    {
                        adp.Fill(dt);
                    }
                }
                cn.Close();
            }
            gridControl1.DataSource = dt;
            ColumnReFormat(gridView1);
            gridView1.BestFitColumns();
        }

        private void txtkod_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            CariBilgileri(txtkod.Text);
        }

        private void txtkod_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                CariBilgileri(txtkod.Text);
            }
        }

        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            txtkod.Text = "";
            txtBilgi.Text = "";
            lblCiro2.Text = "0 TL";
            gridControl1.DataSource = null;
        }

        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Rapor rp = new Rapor();
            rp.LoadData(txtkod.Text);
            rp.Print();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            txtkod.Text = "";
            txtBilgi.Text = "";
            lblCiro2.Text = "0 TL";
            gridControl1.DataSource = null;
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            txtkod.Focus();
        }

        private void frmMain_Activated(object sender, EventArgs e)
        {
            txtkod.Focus();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            Rapor rp = new Rapor();
            rp.LoadData(txtkod.Text);
            rp.Print();
        }

    }
}
